home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / prog / graphics / point_set.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  3KB  |  143 lines

  1. #include <LEDA/point_set.h>
  2. #include <LEDA/plane.h>
  3. #include <LEDA/window.h>
  4.  
  5.  
  6. window* Wp;
  7.  
  8. void draw_vor_seg(double x1, double y1, double x2, double y2,double,double)
  9. { Wp->draw_segment(x1,y1,x2,y2,blue); }
  10.  
  11. void draw_triang_seg(double x1, double y1, double x2, double y2)
  12. { Wp->draw_segment(x1,y1,x2,y2,red); 
  13.  }
  14.  
  15. void infi_pt(double x1, double y1, double x2, double y2, double *x, double* y)
  16. { vector v(x2,y2);
  17.   v = v.norm();
  18.   *x = x1 + 1000 * v[0];
  19.   *y = y1 + 1000 * v[1];
  20.  }
  21.  
  22.  
  23. void draw_polygon(window& W, point_set<string>& S, list<ps_item>& P)
  24. { list_item it;
  25.   forall_items(it,P)
  26.   { point p =  S.key(P[it]);
  27.     point q =  S.key(P[P.cyclic_succ(it)]);
  28.     W.draw_segment(p,q);
  29.    }
  30.  }
  31.  
  32. main()
  33. {
  34.  
  35.   window W;
  36.  
  37.   Wp = &W;
  38.  
  39.   int node_width = 4;
  40.   int line_width = 1;
  41.   int grid_width = 0;
  42.  
  43.  
  44.   W.set_mode(xor_mode);
  45.  
  46.   panel P("POINT SET");
  47.  
  48.   P.text_item("Use the left mouse button to insert points. If a shift ");
  49.   P.text_item("key is pressed simultaneously the nearest neighbor of  ");
  50.   P.text_item("the current mouse position is computed and displayed.  ");
  51.   P.text_item("An iso-oriented query rectangle can by defined using   ");
  52.   P.text_item("the middle button twice. An orthogonal range query is  ");
  53.   P.text_item("performed and all points lying inside the rectangle    ");
  54.   P.text_item("are deleted. Click the right button to quit.           ");
  55.  
  56.   P.int_item("line width",line_width,1,5);
  57.   P.int_item("node width",node_width,1,10);
  58.   P.int_item("GRID",grid_width,0,8,1);
  59.  
  60.   P.open();
  61.  
  62.   W.clear();
  63.   W.set_node_width(node_width);
  64.   W.set_line_width(line_width);
  65.   W.set_grid_mode(grid_width);
  66.  
  67.   point_set<string> S;
  68.  
  69.   double        x,y,x1,y1;
  70.   int           button=0;
  71.   bool          voro = false;
  72.   ps_item       nearest_it=nil;
  73.   point         p; 
  74.   list<ps_item> Pol;
  75.  
  76.  
  77.  
  78.   while (button !=3 )
  79.   {  
  80.      button =  W.read_mouse(x,y);
  81.  
  82.      if (nearest_it) W.draw_edge_arrow(p,S.key(nearest_it));
  83.  
  84.      if (!Pol.empty()) draw_polygon(W,S,Pol); 
  85.  
  86.      if (voro)  S.trace_voronoi_edges(draw_vor_seg,infi_pt);
  87.  
  88.      Pol.clear();
  89.      nearest_it = nil;
  90.      voro = false;
  91.  
  92.      p = point(x,y);
  93.  
  94.      switch(button) {
  95.  
  96.      case 1: { string s("point (%f,%f)",p.xcoord(),p.ycoord());
  97.                S.insert(p,s);
  98.                W.draw_filled_node(p);
  99.                break;
  100.               }
  101.  
  102.  
  103.      case 2: { W.read_mouse_rect(x,y,x1,y1);
  104.                list<ps_item> L = S.range_search(x,x1,y,y1);
  105.                ps_item it;
  106.                forall(it,L) 
  107.                { cout << "delete " << S.inf(it) << "\n";
  108.                  W.draw_filled_node(S.key(it));
  109.                  S.del_item(it);
  110.                 }
  111.                cout.flush();
  112.                break;
  113.               }
  114.  
  115.      case -1:  { nearest_it = S.nearest_neighbor(p);
  116.                  if (nearest_it!=nil) 
  117.                   { W.draw_edge_arrow(p,S.key(nearest_it));
  118.                     cout << "Nearest " << S.inf(nearest_it) << "\n";
  119.                    }
  120.                  else cout << "Empty point set.\n";    
  121.                  cout.flush();
  122.                  break;
  123.                 }
  124.  
  125.      case -2:  { S.trace_voronoi_edges(draw_vor_seg,infi_pt);
  126.                  voro = true;
  127.                  break;
  128.                 }
  129.  
  130.      case -3:  { Pol = S.convex_hull();
  131.                  draw_polygon(W,S,Pol);
  132.                  break;
  133.                 }
  134.  
  135.      } //switch
  136.  
  137.  
  138. } //for
  139.  
  140. return 0;
  141.  
  142. }
  143.